import {
  imageUrl
} from "../utils/imageutil"
Page({

  data: {
    imageUrl,
    nav_list: [{
      title: '全部',
      type: 0
    }, {
      title: '待付款',
      type: 1
    }, {
      title: '待接单',
      type: 2
    }, {
      title: '待服务',
      type: 3
    }, {
      title: '服务中',
      type: 4
    }, {
      title: '待评价',
      type: 5
    }],
    nav_curr: 0,
    page: 1,
    flag: true,
    list: [],
    is_water: false,
    is_reward:false,
    reward_num:'',
    vehgive_id:'',
    rand: 0,
  },

  onLoad(options) {
    if (options.navcurr) {
      this.setData({
        nav_curr: options.navcurr
      })
    }
    this.user_order_vehicle()
  },

  onShow() {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.back) {
      this.setData({
        back: false,
        page: 1,
        list: [],
        flag: true
      })
      this.user_order_vehicle()
    }
  },

  onReachBottom() {
    this.user_order_vehicle()
  },

  user_order_vehicle() {
    if (!this.data.flag) {
      return
    }
    this.setData({
      flag: false
    })
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_order_vehicle', {
      page: this.data.page,
      size: 10,
      status: this.data.nav_curr
    }).then(res => {
      wx.stopPullDownRefresh()
      if (res.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.list.concat(res.data.data);
        this.setData({
          flag: true,
          list: arr,
          page: this.data.page + 1
        })
      }
    })
  },

  vehicle_cancel(e) {
    let id = e.currentTarget.dataset.id
    let good_id = e.currentTarget.dataset.good_id
    let url = '/index.php/api/app/user/sorting/ouyoung_api/vehicle_cancel'
    if(good_id==3){
      url = '/index.php/api/ouyoung/contract/sorting/ouyapi/clean_cancel'
    }
    getApp().globalData.posts(url, {
      id
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        this.setData({
          flag: true,
          page: 1,
          list: []
        })
        this.user_order_vehicle()
      })
    })
  },

  updata_list() {
    this.setData({
      flag: true,
      page: 1,
      list: [],
      is_water: false,
      rand: 0,
    })
    this.user_order_vehicle()
  },

  reward(e){
    let vehgive_id = e.currentTarget.dataset.id;
    this.setData({
      is_reward:true,
      vehgive_id
    })
  },
  change_reward_num(e){
    this.setData({
      reward_num:e.detail.value
    })
  },
  change_is_reward(){
    this.setData({
      is_reward:false
    })
  },
  vehgive(){
    let order_id = this.data.vehgive_id;
    if(!this.data.reward_num){
      getApp().showToast('请输入打赏金额')
      return
    }
    getApp().globalData.posts('/api/app/user/sorting/ouyoung_api/vehgive',{
      price:this.data.reward_num,
      order_id
    }).then((res)=>{
      let data = res.data.data; 
      if(res.data.code==1){
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            getApp().showModal('打赏成功').then(()=>{
              this.setData({
                is_reward: false
              })
            })
          }
        })
      }else{
        getApp().showModal(res.data.msg)
      }
    })
  },

  Vehiclepayv2(e) {
    let order_id = e.currentTarget.dataset.id
    getApp().globalData.post({
      data: {
        do: 'Vehiclepayv2',
        a: 'wxapp2',
        order_id
      }
    }).then(res => {
      let data = res.data.data;
      if (res.data.code == 1) {
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            this.setData({
              is_water: true,
              rand: data.rand,
            })
          },
          fail: () => {
            getApp().showToast('支付失败！')
          }
        })
      } else {
        getApp().showModal(res.data.msg)
      }
    })
  },

  Hytcpayv2(e) {
    let order_id = e.currentTarget.dataset.id
    getApp().globalData.post({
      data: {
        do: 'Hytcpayv2',
        a: 'wxapp2',
        order_id
      }
    }).then(res => {
      let data = res.data.data;
      if (res.data.code == 1) {
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            this.setData({
              is_water: true,
              rand: data.rand,
            })
          },
          fail: () => {
            getApp().showToast('支付失败！')
          }
        })
      } else {
        getApp().showModal(res.data.msg)
      }
    })
  },

  Bjtcpayv2(e){
    let order_id = e.currentTarget.dataset.id
    getApp().globalData.post({
      data: {
        do: 'Bjtcpayv2',
        a: 'wxapp2',
        order_id
      }
    }).then(res => {
      let data = res.data.data;
      if (res.data.code == 1) {
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          timeStamp: data.timeStamp,
          success: () => {
            this.setData({
              is_water: true,
              rand: data.rand,
            })
          },
          fail: () => {
            getApp().showToast('支付失败！')
          }
        })
      } else {
        getApp().showModal(res.data.msg)
      }
    })
  },

  phoneCall(e) {
    let tel = e.currentTarget.dataset.tel;
    getApp().phoneCall(tel)
  },


  change_nav_curr(e) {
    let nav_curr = e.currentTarget.dataset.type;
    if (nav_curr == this.data.nav_curr) {
      return
    }
    this.setData({
      nav_curr,
      page: 1,
      list: [],
      flag: true
    })
    this.user_order_vehicle()
  },

  to_detail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: './detail?id=' + id,
    })
  },

  to_evaluate(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: './evaluate?id=' + id,
    })
  }

})